/*
 *@package wanyueAdmin -- Be based on layuiAdmin-v1.2.1
 *@author zeng sheng
 *@date 2019/04/30
 */

function Wanyue(config) {
  var call = function(s, a, b, c) {
      return typeof s == 'function' ? s(a, b, c) : s;
  };
  this.layuiAdmin = {
      set: {
          base: 'https://image.buslive.cn/wfs/static2/yue_welfare_admin/static/layuiadmin/'
          , version: ''
          , theme: 10
      },
      modules: [
          'index', 'laytpl'
      ],
      get: function(attr) {
          configuried = (typeof config != 'undefined') && (typeof config[attr] != 'undefined');
          return configuried ? config[attr] : this.set[attr];
      },
      tpl: function(tpl) {
          var version = this.get('version').toString();
          return this.get('base')+'tpl/wanyue/'+tpl+(version.length > 0 ? '?_='+version : '');
      },
      req: function(that) {
          var _this = this;
          layui.admin.req({
              url: that.url
              , type: 'post'
              , data: that.data
              , success: function(result) {
                  if (typeof that.next != 'undefined') {
                      call(that.next, result, layui.layer);
                  } else {
                      _this.msg({msg: result.msg, error: result.code});
                      if (result.code == 0) {
                          setTimeout(function() {
                              var index = parent.layui.layer.getFrameIndex(window.name);
                              parent.layui.layer.close(index); 
                              typeof parent.layui.table != 'undefined' && parent.layui.table.reload('LAY-app-content-list'); 
                          }, 1000);
                      }
                  }
              }
          });
      },
      msg: function(item) {
          layui.layer.msg(item.msg, {
              offset: '15px'
              , icon: item.error == 0 ? 1 : 2 
              , time: 2000
          });
      },
      open: function(item, that) {
          layui.layer.open({
              type: typeof item.content != 'undefined' ? 1 : 2
              , offset: item.offset || '0px'
              , title: item.title
              , content: item.url || item.content
              , maxmin: true
              , area: item.area || ['100%', '100%']
          });
      },
      render: function(that) {
          var _this = this;
          layui.config({
               base: _this.get('base')
              , version: _this.get('version')
          }).extend({
              index: 'lib/index'
          }).use(that.modules, function() {
               layui.$('body').load(that.file, function() {
                   layui.$.each(that.data, function(attr, value) {
                       that.data[attr] = call(value, layui);
                   })
                   layui.$.each(that.list, function(index, item) {
                       layui.laytpl(layui.$(item.ele).html()).render(that.data, function(html) {
                           var ele = layui.$(item.ele).parent();
                           ele.html(html);
                           !item.handler || item.handler(layui, ele);
                       });
                   })
               });
          });
      },
      start: function(data, settings) {
          var _this = this;
          this.modules.push('admin', 'layer');
          if (typeof data.page_url == 'undefined') {
              if (typeof data.menus[0]['list'] == 'undefined') {
                  data.page_url = '';
              } else {
                  data.page_url = call(data.menus)[0]['list'][0]['url'] || '';
              }
          }
          this.render({
              file: this.tpl('start.html')
              , modules: this.modules
              , data: data
              , list: [{
                  ele: '#start-tpl'
                  , handler: function(layui, e) {
                      layui.admin.initTheme(data.theme || _this.set.theme);
                      var fiexible = function(right) {
                          var that = layui.$('#LAY_app_flexible');
                          if (that.hasClass('layui-icon-shrink-right') && right) {
                              layui.$('#LAY_app').addClass('layadmin-side-shrink');
                              that.removeClass('layui-icon-shrink-right').addClass('layui-icon-spread-left');
                          } else {
                              layui.$('#LAY_app').removeClass('layadmin-side-shrink');
                              that.removeClass('layui-icon-spread-left').addClass('layui-icon-shrink-right');
                          }
                      };
                      layui.admin.events.flexible = function() {
                          fiexible(true);
                      };
                      layui.$('.layui-nav-tree').find('.layui-nav-item').children('a:not("dl a")').click(function() {
                          var nav = layui.$(this).parent();
                          if (!nav.hasClass('layui-nav-itemed')) {
                              nav.addClass('layui-nav-itemed').siblings().removeClass('layui-nav-itemed');
                          } else {
                              nav.removeClass('layui-nav-itemed');
                          }
                          fiexible(false);
                      });
                      e.find('.layui-layout-right').find('li').first().mousemove(function() {
                          layui.$(this).next().find('span').removeClass('mored').parent().next().removeClass('layui-show');
                      }).next().mousemove(function() {
                          layui.$(this).find('span').addClass('mored').parent().next().addClass('layui-show');
                      })
                      e.find('.layui-nav-child').mouseleave(function() {
                          layui.$(this).prev().find('span:eq(0)').removeClass('layui-nav-mored')
                          .parent().next().removeClass('layui-show');
                      });
                      layui.admin.events.exit = function(e) {
                          layui.layer.confirm('确定退出吗？', {offset: '10%'}, function() {
                              window.location.assign(e.attr('data-url'));
                          });
                      };
                  }
              }]
          });
      },
      list: function(data, settings) {
          var _this = this;
          this.modules.push('form');
          this.modules.push('table');
          this.modules.push('layer');
          if (typeof settings.datetime != 'undefined') {
              this.modules.push('laydate');
          }
          this.render({
              file: this.tpl('list.html')
              , modules: this.modules
              , data: data
              , list: [{
                  ele: '#analysis-tpl'
                  , handler: function(layui) {
                      if (typeof settings.analysis != 'undefined') {
                          if (typeof settings.analysis.url != 'undefined') {
                              layui.$.getJSON(settings.analysis.url, function(result) {
                                  var html = '';
                                  layui.each(result.data, function(index, item) {
                                      html += "<span style='margin-left:10px;font-weight:bold;'>"+item.title+"</span> : <span style='color:#FFB800;'>"+item.value+"</span>";
                                  });
                                  layui.$('#analysis-wrapper').html(html);
                              });
                          }
                      }
                  }
                }, {
                  ele: '#actions-tpl'  
                  , handler: function(layui) {
                      layui.each(settings.fields, function(index, item) {
                          if (typeof item.preview != 'undefined') {
                              if (item.preview.type == 'photo') {
                                  item['templet'] = '<script type="text/html"><a class="layui-btn layui-btn-normal layui-btn-xs" layadmin-event="'+item.field+'" href="javascript:;" data-title="{{d.'+[item.preview.title]+'}}" data-id="{{d.id}}">查看</a><\/script>'
                                  layui.admin.events[item.field] = function(elem) {
                                       layui.$.getJSON(item.preview.url.replace('__ID__', layui.$(elem).attr('data-id')), function(result) {
                                           layui.layer.photos({
                                               title: layui.$(elem).attr('data-title')
                                               , photos: {data: result.data}
                                               , offset: '10%'
                                               , area: 'auto'
                                               , anim: 4 
                                               , shade: 0.01
                                               , closeBtn: 1
                                           });
                                       });
                                  }
                              } else if(item.preview.type == 'open') {
                                  if (typeof item.preview.btn != 'undefined') {
                                      item['templet'] = '<script type="text/html"><a class="layui-btn layui-btn-normal layui-btn-xs" layadmin-event="'+item.field+'" href="javascript:;" data-title="'+item.preview.title+'" data-id="{{d.id}}">查看</a><\/script>'
                                  } else {
                                      item['templet'] = '<script type="text/html"><a style="color:#1E9FFF" layui-btn-xs" layadmin-event="'+item.field+'" href="javascript:;" data-title="{{d.'+[item.preview.title]+'}}" data-id="{{d.id}}">{{d.'+[item.preview.title]+'}}</a><\/script>'
                                  }
                                  layui.admin.events[item.field] = function(elem) {
                                      _this.open({title: item.preview.subtitle || layui.$(elem).attr('data-title'), url: item.preview.url.replace('__ID__', layui.$(elem).attr('data-id'))});
                                  }
                              }
                          }
                      })
                      if (typeof settings.toolbars != 'undefined' && typeof settings.toolbars.list != 'undefined') {
                          var s = '';
                          layui.$.each(settings.toolbars.list, function(index, item) {
                              if (typeof item.hidden != 'undefined' && item.hidden) {
                                  return true;
                              }
                              if (typeof item.type != 'undefined' && item.type == 'mutiple') {
                                  s += '{{# if(d.'+item.when[0]+') { }}'; 
                                  s += '<a class="layui-btn '+item.btn[0]+' layui-btn-xs" data-index="0" data-title-list="'+(typeof item.title == 'object' ? item.title : [item.title, item.title])+'" data-btn-list="'+item.btn+'" data-icon-list="'+item.icon+'" lay-event="'+item.handler+'"><i class="layui-icon '+item.icon[0]+'"></i>'+(typeof item.title == 'string' ? item.title : item.title[0])+'</a>';
                                  s += '{{# } else if(d.'+item.when[1]+') { }}'; 
                                  s += '<a class="layui-btn '+item.btn[1]+' layui-btn-xs" data-index="1" data-title-list="'+(typeof item.title == 'object' ? item.title : [item.title, item.title])+'" data-btn-list="'+item.btn+'" data-icon-list="'+item.icon+'" lay-event="'+item.handler+'"><i class="layui-icon '+item.icon[1]+'"></i>'+(typeof item.title == 'string' ? item.title : item.title[1])+'</a>';
                                  s += '{{# } else { }}';
                                  s += '<a class="layui-btn layui-btn-disabled layui-btn-xs" lay-event=""><i class="layui-icon '+item.icon[0]+'"></i>'+item.title[0]+'</a>';
                                  s += '{{# } }}';
                              } else {
                                  var when = typeof item.when !='undefined' ? 'd.'+item.when : true;
                                  s += '{{# if('+when+') { }}'; 
                                  s += '<a class="layui-btn '+item.btn+' layui-btn-xs" lay-event="'+item.handler+'"><i class="layui-icon '+item.icon+'"></i>'+item.title+'</a>';
                                  s += '{{# } else { }}';
                                  s += '<a class="layui-btn layui-btn-disabled layui-btn-xs" lay-event="'+item.handler+'"><i class="layui-icon '+item.icon+'"></i>'+item.title+'</a>';
                                  s += '{{# } }}';
                              }
                              //}
                          });
                          layui.$('#toolbar-list').html(s);
                          settings.fields.push({title: '操作', minWidth: settings.toolbars.width || 460, align: 'center', fixed: 'right', toolbar: '#toolbar-list'});
                      }
                      layui.table.render({
                          elem: '#LAY-app-content-list'
                          , url: settings.url
                          , data: settings.data
                          , cols: [settings.fields]
                          , page: {theme: '#1E9FFF'}
                          , limit: 30
                          , limits: [30, 60, 90]
                      });

                      var open = function(self, item, that) {
                          var e = layui.$(self);
                          var index = e.attr('data-index');
                          var title = typeof item.title == 'object' ? item.title[index] : item.title;
                          if (e.hasClass('layui-btn-disabled')) {
                              return false;
                          }
                          if (item.type == 'confirm') {
                              layui.layer.confirm('确定'+title+'吗？', {offset: '100px', area: ['30%', '30%']}, function() {
                                  _this.req({
                                      url: typeof item.url == 'object' ? item.url[index] : item.url
                                      , data: item.data
                                      , next: function(result, layer) {
                                          var yes = result.code == 0;
                                          layer.msg(result.msg, {
                                              offset: '15px'
                                              , icon: result.code == 0 ? 1 : 2 
                                              , time: 2000
                                          });
                                          if (yes) {
                                              if (typeof item.updated != 'undefined' && typeof item.updated == 'object') {
                                                  var original = e.attr('data-index');
                                                  var index = original == 0 ? 1 : 0;
                                                  var btn = e.attr('data-btn-list').split(',')[index];
                                                  var icon = e.attr('data-icon-list').split(',')[index];
                                                  var title = e.attr('data-title-list').split(',')[index];
                                                  e.attr('data-index', index).removeClass(e.attr('class'))
                                                  .addClass('layui-btn '+btn+' layui-btn-xs')
                                                  .html('<i class="layui-icon '+icon+'">'+title+'</i>');
                                                  that.update(item.updated.fields[original]);
                                                  call(item.updated.trigger, e);
                                                  return true;
                                              } 
                                              if (typeof item.del != 'undefined' && item.del) {
                                                  that.del();
                                                  return true;
                                              }
                                          }
                                      }
                                  });
                              });
                          } else if (item.type == 'edit')  {
                              layui.layer.open({
                                  type: 2
                                  , title: item.title
                                  , content: item.url
                                  , maxmin: true
                                  , moveOut: true
                                  , offset: '60px'
                                  , area: item.area || ['100%', '100%']
                                  , btn: ['提交', '取消']
                                  , yes: function(index, layerno) {
                                      var iframeWindow = window['layui-layer-iframe'+ index]
                                          , submit = layerno.find('iframe').contents().find("#layuiadmin-app-form-submit");
                                      iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data) {
                                          _this.req({
                                              url: item.url
                                              , data: data.field
                                              , next: function(result, layer) {
                                                  var yes = result.code == 0;
                                                  layer.msg(result.msg, {
                                                      offset: '15px'
                                                      , icon: result.code == 0 ? 1 : 2 
                                                      , time: 2000 });
                                                  if (yes) {
                                                      setTimeout(function() {
                                                          layui.layer.close(index);
                                                      }, 1000);
                                                      that.update(item.updated(data.field));
                                                  }
                                              }
                                          });
                                      });
                                      submit.trigger('click');
                                  }
                              });
                          } else {
                              _this.open(item, that);
                          }
                      };
                      layui.table.on('tool(LAY-app-content-list)', function(obj) {
                          var self = this;
                          var data = obj.data;
                          if (typeof settings.csrf != 'undefined') {
                              data[settings.csrf.name] = settings.csrf.value;
                          }
                          if (obj.event.length > 0) {
                              eval('var handler = '+obj.event);
                              call(handler, function(item) {
                                  item.data = data;
                                  return open(self, item, obj);
                              }, data, layui);
                          }
                      })
                      layui.$('.action-list').click(function() {
                          var idx = layui.$(this).index();
                          var checked = layui.table.checkStatus('LAY-app-content-list');
                          if (checked.data.length > 0) {
                              data.checked = checked.data;
                          }
                          if (data.actions[idx].url) {
                              layui.event.call('', 'form', 'submit(LAY-app-contlist-export)', {url: data.actions[idx].url});
                          }
                          call(data.actions[idx].handler, _this, checked.data, layui);
                      });
                      call(settings.handler, layui);

                  }
              }, {
                  ele: '#form-tpl'  
                  , handler: function(layui, ele) {
                      var field = {};
                      layui.form.on('submit(LAY-app-contlist-search)', function(data) {
                          field = data.field;
                          layui.table.reload('LAY-app-content-list', {
                              where: field
                          });
                          if (typeof settings.analysis != 'undefined' && typeof settings.analysis.url != 'undefined') {
                              var url = settings.analysis.url;
                              layui.each(field, function(attr, value) {
                                  var sp = url.indexOf('?') > -1 ? '&' : '?';
                                  url += sp + attr+'='+value;
                              });
                              layui.$.getJSON(url, function(result) {
                                  var html = '';
                                  layui.each(result.data, function(index, item) {
                                      html += "<span style='margin-left:10px;font-weight:bold;'>"+item.title+"</span> : <span style='color:#FFB800;'>"+item.value+"</span>";
                                  });
                                  layui.$('#analysis-wrapper').html(html);
                              });
                          }
                      });
                      layui.form.on('submit(LAY-app-contlist-export)', function(data) {
                          layui.each(field, function(attr, value) {
                              var sp = data.url.indexOf('?') > -1 ? '&' : '?';
                              data.url += sp + attr+'='+value;
                          });
                          window.location.assign(data.url);
                      });
                      layui.form.on('submit(LAY-app-contlist-export2)', function(obj) {
                          var url = layui.$(obj.elem).attr('data-url');
                          if (!call(data.export.handler, obj, _this, layui)) {
                              return false;
                          }
                          layui.event.call(this, 'form', 'submit(LAY-app-contlist-search)', obj);
                          layui.each(obj.field, function(attr, value) {
                              var sp = url.indexOf('?') > -1 ? '&' : '?';
                              url += sp + attr+'='+value;
                          });
                          layui.layer.msg('正在导出，请稍后...', {
                              offset: '15px'
                              , icon: 1 
                              , time: 8000
                          });
                          setTimeout(function() {
                              window.location.assign(url);
                          }, 3000);
                          return false;
                      });
                      if (typeof settings.datetime != 'undefined') {
                          layui.each(settings.datetime, function(index, item) {
                              layui.laydate.render({
                                  elem: 'input[name='+item+']'
                                  , type: 'datetime'
                                  , theme: '#1E9FFF'
                                  , zIndex: 99999999
                              });
                          });
                      }
                      layui.form.render();
                  }
              }]
          });
      },
      tree: function(settings) {
          var _this = this;
          this.modules.push('tree');
          this.modules.push('util');
          this.render({
              file: this.tpl('tree.html')
              , modules: this.modules
              , data: {
                  ele: 'tree'
                  , title: settings.title || ''
                  , action: settings.action
                  , submit: (typeof settings.onsubmit != 'undefined') ? true : false
              }
              , list: [{
                  ele: '#tree-tpl'
                  , handler: function(layui, ele) {
                      layui.layer.config({offset:'10%'});
                      var render = function(options, checked) {
                          layui.tree.render(options);
                          layui.tree.setChecked(options.id, checked);
                      };
                      var options = settings.options || {};
                      options.elem = '#tree';
                      options.id = 'tree001';
                      options.accordion = true;
                      if (typeof settings.oncheck != 'undefined') {
                          options.oncheck = function(that) {
                              call(settings.oncheck, that, _this, layui);
                          }
                      }
                      if (typeof settings.operate != 'undefined') {
                          options.operate = function(that) {
                              call(settings.operate, that, _this, layui);
                          };
                      }
                      if (typeof settings.action != 'undefined') {
                          layui.$(ele).find('#btn-action').click(function() {
                              call(settings.action.handler, _this, layui);
                          });
                      };
                      if (typeof settings.onsubmit != 'undefined') {
                          layui.$(ele).find('#btn-submit').click(function() {
                              call(settings.onsubmit, layui.tree.getChecked(options.id), _this, layui);
                          });
                      }
                      if (typeof settings.options.data != 'undefined') {
                          render(options, settings.checked);
                      }
                      if (typeof settings.url != 'undefined') {
                          layui.$.getJSON(settings.url, function(result) {
                              options.data = result.data;
                              render(options, settings.checked);
                              return true;
                          });
                      }
                  } 
              }]
          });
      },
      form: function(data, settings) {
          var _this = this;
          var _data = data;
          this.modules.push('form');
          this.modules.push('admin');
          if (typeof settings.upload != 'undefined') {
              this.modules.push('upload');
          }
          if (typeof settings.datetime != 'undefined') {
              this.modules.push('laydate');
          }
          this.render({
              file: this.tpl('form.html')
              , modules: this.modules
              , data: data
              , list: [{
                  ele: '#form-tpl'
                  , handler: function(layui) {
                      if (typeof settings.upload != 'undefined') {
                          layui.each(settings.upload, function(index, item) {
                              var upload = '#upload-'+item.name;
                              var preview = '#preview-'+item.name;
                              var input = 'input[name='+item.name+']';
                              var re = function(yes) {
                                  layui.$(upload).html('<i class="layui-icon">&#xe67c;</i>重新上传');
                                  !yes || layui.$(preview).removeClass('layui-btn-disabled').addClass('layui-btn-warm');
                              };
                              (layui.$(input).val().length > 0) && re(layui.$(preview).length > 0);
                              if (layui.$(preview).length > 0) {
                                  layui.$(preview).click(function() {
                                      layui.layer.photos({
                                          photos: {
                                             "title": '预览', 
                                             "data": [{"src": layui.$(input).val()}]
                                          }
                                          , offset: '10%'
                                          , area: 'auto'
                                          , anim: 4 
                                          , shade: 0.01
                                          , closeBtn: 1
                                      });
                                  });
                              } else {
                                  var e = layui.$('#download-'+item.name);
                                  if (e.length > 0) {
                                      e.click(function() {
                                          e.html('<i class="layui-icon layui-icon-download-circle"></i>下载中...');
                                          setTimeout(function() {
                                              window.location.assign(e.attr('data-url'));
                                          }, 2000);
                                          setTimeout(function() {
                                              e.html('<i class="layui-icon layui-icon-download-circle"></i>示例文件');
                                          }, 5000);
                                      });
                                  }
                              }
                              layui.upload.render({
                                  elem: upload 
                                  , accept: 'file'
                                  , exts: item.exts.join('|') 
                                  , auto: false
                                  , choose: function(obj) {
                                      layui.$(upload).html('<i class="layui-icon">&#xe67c;</i>上传中...');
                                      obj.preview(function (index, file, result) {
                                          layui.$(input).val(result);
                                          setTimeout(function() {
                                              re(layui.$(preview).length > 0);
                                              layui.layer.msg('成功上传: '+file.name, {icon: 1, time: 3000});
                                          }, 2000);
                                      });
                                  }
                              });
                          });
                      }
                      if (typeof settings.datetime != 'undefined') {
                          layui.each(settings.datetime, function(index, item) {
                              layui.laydate.render({
                                  elem: 'input[name='+item+']'
                                  , type: 'datetime'
                                  , theme: '#1E9FFF'
                                  , zIndex: 99999999
                              });
                          });
                      }
                      call(settings.handler, layui, _this);
                      layui.form.render();
                  } 
              }, {
                  ele: '#submit-tpl'
                  , handler: function(layui, e) {
                      if (typeof settings.type != 'undefined' && settings.type == 'edit') {
                          layui.$(e).children().hide();
                      }
                      layui.form.on('submit(layuiadmin-app-form-submit)', function(obj) {
                          var data = obj.field
                          if (typeof parent.layui.table != 'undefined') {
                              var checked = parent.layui.table.checkStatus('LAY-app-content-list');
                              if (checked.data.length > 0) {
                                  if (settings.checked) {
                                      var i = settings.checked;
                                      data.checked = [];
                                      layui.each(checked.data, function(index, item) {
                                          data.checked.push(item[i]);
                                      });
                                  } else {
                                      data.checked = JSON.stringify(checked.data);
                                  }
                              }
                          }
                          var s = {url: settings.url.submit, data: data};
                          if (typeof settings.url.jump != 'undefined') {
                              s.next = function(result, layer) {
                                  var yes = result.code == 0;
                                  layui.layer.msg(result.msg, {
                                      offset: '15px'
                                      , icon: result.code == 0 ? 1 : 2 
                                      , time: 2000
                                  });
                                  if (yes) {
                                      setTimeout(function() {
                                          if (typeof settings.jumpToParent != 'undefined' && settings.jumpToParent) {
                                              parent.window.location.assign(settings.url.jump);
                                          } else {
                                              window.location.assign(settings.url.jump);
                                          }
                                      }, 2000);
                                  }
                              };
                          }
                          _this.req(s);
                      });
                  }
              }]
          });
      },
      login: function(data, settings) {
          var _this = this;
          this.modules.push('form');
          this.render({
              file: this.tpl('login.html')
              , modules: this.modules
              , data: data
              , list: [{
                  ele: '#login-tpl'
                  , handler: function(layui, ele) {
                      layui.form.on('submit(LAY-user-login-submit)', function(obj) {
                          var data = obj.field;
                          //layui.$(obj.elem).prop('disabled', true);
                          setTimeout(function() {
                              _this.req({
                                  url: settings.url.submit,
                                  data: data, 
                                  next: function(result, layer) {
                                      layer.msg(result.msg , {
                                          offset: '15px'
                                          , icon: result.code == 0 ? 1 : 2 
                                          , time: 3000
                                      });
                                      if (result.code == 0) {
                                          ele.find('.login-btn').text('登录中...');
                                          setTimeout(function() {
                                              window.location.assign(settings.url.jump);
                                          }, 1000);
                                      } 
                                  }
                              });
                          }, 1000);
                      });
                  }
              }]
          });
      }
   };
}

